﻿using System;

namespace Experiments.MichaelKappel.Net.Tilt.Axis
{
  public class AxisGraphBasicInfo
    {
        public AxisGraphBasicInfo()
        {
            MappingX = AxisType.X;
            MappingY = AxisType.Y;
            MappingZ = AxisType.Z;

            AxisOffset = new AxisDegreeInfo();
            AxisRaw = new AxisInfo();
            Axis = new AxisDegreeInfo();
        }
        public AxisDegreeInfo Axis
        {
            get;
            set;
        }
        public AxisDegreeInfo AxisOffset
        {
            get;
            set;
        }
        public AxisInfo AxisRaw
        {
            get;
            set;
        }
        public AxisType? MappingX
        {
            get;
            set;
        }
        public AxisType? MappingY
        {
            get;
            set;
        }
        public AxisType? MappingZ
        {
            get;
            set;
        }
        public Boolean InverseX
        {
            get;
            set;
        }
        public Boolean InverseY
        {
            get;
            set;
        }
        public Boolean InverseZ
        {
            get;
            set;
        }
        protected Double GetMappedAxis(AxisType? mapping)
        {
            if (!mapping.HasValue)
                return 0;
            else if (mapping.Value == AxisType.X)
                return AxisRaw.X;
            else if (mapping.Value == AxisType.Y)
                return AxisRaw.Y;
            else
                return AxisRaw.Z;
        }
        public static Axis.AxisType? GetNextAxis(Axis.AxisType?  axis)
        {
            Axis.AxisType? result = null;

            if (axis == null)
                result = AxisType.X;
            else if (axis == AxisType.X)
                result = AxisType.Y;
            else if (axis == AxisType.Y)
                result = AxisType.Z;
            else
                result = null;

            return result;
        }

        public void Calibrate(AxisDegreeInfo offset)
        {
            AxisDegreeInfo result = AxisDegreeInfo.ConvertToDegreeInfo(
                                new AxisInfo(GetMappedAxis(MappingX),
                                        GetMappedAxis(MappingY),
                                        GetMappedAxis(MappingZ)));
   
            result.X = (result.X * -1) + offset.X;
            result.Y = (result.Y * -1) + offset.Y;
            result.Z = (result.Z * -1) + offset.Z;

            AxisOffset = result;

        }
        public void Reset(AxisDegreeInfo axis)
        {
            InverseX = false;
            InverseY = false;
            InverseZ = false;
            MappingX = AxisType.X;
            MappingY = AxisType.Y;
            MappingZ = AxisType.Z;
            AxisOffset = axis;
        }
    }
}
